package com.jlf.core.engine.conf;

import com.jlf.core.engine.meta.MetaRepository;
import com.jlf.core.engine.persistence.SchemaCriterionPersistence;
import com.jlf.core.engine.persistence.SchemaDomainPersistence;
import com.jlf.core.engine.persistence.SchemaFieldPersistence;
import com.jlf.core.engine.persistence.SchemaIndexPersistence;
import com.jlf.core.engine.persistence.impl.SchemaCriterionPersistenceImpl;
import com.jlf.core.engine.persistence.impl.SchemaFieldPersistenceImpl;
import com.jlf.core.engine.persistence.impl.SchemaIndexPersistenceImpl;
import com.jlf.core.engine.server.SchemaCriterionServer;
import com.jlf.core.engine.server.SchemaDomainServer;
import com.jlf.core.engine.driver.SchemaDriveServer;
import com.jlf.core.engine.server.SchemaFieldServer;
import com.jlf.core.engine.server.SchemaIndexServer;
import com.jlf.core.engine.server.impl.SchemaCriterionServerImpl;
import com.jlf.core.engine.server.impl.SchemaDomainServerImpl;
import com.jlf.core.engine.driver.impl.SchemaDriveServerImpl;
import com.jlf.core.engine.server.impl.SchemaFieldServerImpl;
import com.jlf.core.engine.persistence.impl.SchemaDomainPersistenceImpl;
import com.jlf.core.engine.server.impl.SchemaIndexServerImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.sql.DataSource;

/**
 * 驱动引擎的自动配置
 *
 * @author wujr
 * 2023/7/7
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/7 1.0 新增]
 */
@Configuration
//@MapperScan(basePackages = {"com.jlf.core.engine.dao"})
public class EngineAutoConfiguration {
    @Bean
    public SchemaDomainPersistence schemaDomainPersistence(){
        return new SchemaDomainPersistenceImpl();
    }
    @Bean
    public SchemaFieldPersistence schemaFieldPersistence(){
        return new SchemaFieldPersistenceImpl();
    }

    @Bean
    public SchemaIndexPersistence schemaIndexPersistence(){
        return new SchemaIndexPersistenceImpl();
    }

    @Bean
    public SchemaCriterionPersistence schemaCriteriaPersistence(){
        return new SchemaCriterionPersistenceImpl();
    }
    @Bean
    public SchemaDomainServer schemaDomainServer(SchemaDomainPersistence schemaDomainPersistence,
                                                 SchemaFieldPersistence schemaFieldPersistence,
                                                 SchemaIndexPersistence schemaIndexPersistence,
                                                 SchemaCriterionPersistence schemaCriterionPersistence){
        return new SchemaDomainServerImpl(schemaDomainPersistence,
                schemaFieldPersistence, schemaIndexPersistence, schemaCriterionPersistence);
    }
    @Bean
    public SchemaFieldServer schemaFieldServer(SchemaDomainPersistence schemaDomainPersistence,
                                               SchemaFieldPersistence schemaFieldPersistence,
                                               MetaRepository metaRepository){
        return new SchemaFieldServerImpl(schemaFieldPersistence, schemaDomainPersistence,
                metaRepository);
    }
    @Bean
    public SchemaIndexServer schemaIndexServer(SchemaIndexPersistence schemaIndexPersistence){
        return new SchemaIndexServerImpl(schemaIndexPersistence);
    }
    @Bean
    public SchemaCriterionServer schemaCriteriaServer(SchemaCriterionPersistence schemaCriterionPersistence){
        return new SchemaCriterionServerImpl(schemaCriterionPersistence);
    }

    @Bean
    public SchemaDriveServer schemaEngineServer(SqlSessionFactory sqlSessionFactory,
                                                DataSourceTransactionManager dataSourceTransactionManager,
                                                TransactionDefinition transactionDefinition,
                                                RequestMappingHandlerMapping requestMappingHandlerMapping,
                                                MetaRepository metaRepository,
                                                SchemaDomainPersistence schemaDomainPersistence,
                                                SchemaFieldPersistence schemaFieldPersistence,
                                                SchemaIndexPersistence schemaIndexPersistence,
                                                SchemaCriterionPersistence schemaCriterionPersistence){
        return new SchemaDriveServerImpl(sqlSessionFactory,
                dataSourceTransactionManager, transactionDefinition,
                requestMappingHandlerMapping,
                metaRepository,
                schemaDomainPersistence, schemaFieldPersistence,
                schemaIndexPersistence, schemaCriterionPersistence);
    }

    @Bean
    public MetaRepository metaRepository(DataSource dataSource){
        return new MetaRepository(dataSource);
    }
}
